// =============================================
// Nginx 模板数据
// 包含静态网站、PHP-FPM、反向代理等配置模板
// =============================================

// Nginx 模板数据
const nginxTemplates = {
    'static-site': {
        name: '静态网站',
        icon: 'fas fa-file',
        content: `server {
    # 监听端口
    listen 80;
    # 服务器域名(多个用空格分隔)
    server_name example.com www.example.com;
    
    # 网站根目录
    root /var/www/html;
    # 默认索引文件
    index index.html index.htm;
    
    # 字符集
    charset utf-8;
    
    # 安全头部设置
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    
    # 根位置配置
    location / {
        # 尝试直接访问文件，找不到则返回index.html
        try_files $uri $uri/ /index.html;
    }
    
    # 静态资源缓存设置
    location ~* \\.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
    
    # 禁止访问隐藏文件
    location ~ /\\. {
        deny all;
        access_log off;
        log_not_found off;
    }
    
    # 错误页面配置
    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
    
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        internal;
    }
}`
    },

    'php-fpm': {
        name: '创建模板',
        icon: 'fas fa-server',
        content: `cd /etc/nginx/conf.d && \\
cat > test.conf <<'EOF'
## 此处添加 nginx 模板


EOF

sudo systemctl restart nginx`
    },

    'reverse-proxy': {
        name: '反向代理',
        icon: 'fas fa-exchange-alt',
        content: `server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    
    # API 反向代理
    location /api/ {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}`
    },
    
    'load-balancer': {
        name: '负载均衡',
        icon: 'fas fa-balance-scale',
        content: `upstream mysvr { 
    server 10.10.10.245:9200;
    ## 负载均衡-热备模式：有2台服务器，一台服务器宕机时，才‮用启‬第二台‮务服‬器给‮供提‬服务。处理‬顺序：AAA突然A挂啦，BBB…
    server 10.10.10.247:9200 backup;  ## 热备     
}

server {
    ## 监听 666 端口，并启用SSL
    listen 666 ssl;
    listen [::]:666 ssl;

    ## 替换为你的域名
    server_name mtab.example.com;

    ## 指定 SSL 证书文件和私钥文件的路径
    ssl_certificate /etc/nginx/keyfile/cert.pem;  
    ssl_certificate_key /etc/nginx/keyfile/key.pem;  
    
    location / {
        ## 指定反向代理的服务地址
        proxy_pass http://mysvr; ## 负载均衡 变量地址（不能和其它服务器相同）
        add_header backendIP $upstream_addr; ## 负载均衡 节点IP
        add_header backendCode $upstream_status; ## 负载均衡 响应状态码
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Range $http_range;
        proxy_set_header If-Range $http_if_range;
        proxy_redirect off;
        proxy_buffering on; #开启缓存
        # 使用 HTTP/1.1 协议与后端服务器通信
        proxy_http_version 1.1;
        client_max_body_size 20000m;
    }
}`
    },

    'ssl-https': {
        name: 'SSL/HTTPS',
        icon: 'fas fa-lock',
        content: `server {
    listen 666 ssl;
    listen [::]:666 ssl;
    server_name pansou.example.com;
    ssl_certificate /etc/nginx/keyfile/cert.pem;
    ssl_certificate_key /etc/nginx/keyfile/key.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers HIGH:!aNULL:!MD5;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options nosniff always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    sendfile on;
    tcp_nopush on;
    client_max_body_size 1024G;
    error_page 404 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/html;
    }
    location / {
        proxy_pass http://10.10.10.251:8110;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Range $http_range;
        proxy_set_header If-Range $http_if_range;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_read_timeout 300s;
        proxy_connect_timeout 300s;
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 16k;
        proxy_busy_buffers_size 24k;
        proxy_max_temp_file_size 0;
    }
}`
    },

    'websocket': {
        name: 'WebSocket 代理',
        icon: 'fas fa-network-wired',
        content: `server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 86400;
    }
}`
    }
};

// Nginx 模板分类
const nginxTemplateCategories = {
    '常用模板': {
        icon: 'fas fa-star',
        templates: ['static-site', 'php-fpm', 'reverse-proxy', 'load-balancer']
    },
    '高级配置': {
        icon: 'fas fa-cogs',
        templates: ['ssl-https', 'websocket']
    }
};